<template>
  <div class="recommend">
    <!-- 推荐页面 -->
    <div class="box" v-for="(item, index) in list" :key="item.id">
      <router-link :to="{ path: '/Caidetails', query: { id: item.id } }">
        <div class="top-img">
          <img class="cai_img" :src="item.image" alt="" />
        </div>
      </router-link>
      <div class="but">
        <!-- 这个是带参数的跳转（item.id）是参数 -->
        <router-link :to="{ path: '/Caidetails', query: { id: item.id } }">
          <span class="title"> {{ item.title }} </span>
        </router-link>
        <div class="but-user">
          <div class="but-user-name">
            <img class="ren_img" :src="item.userimg" alt="" />
            <span class="user">{{ item.username }}</span>
          </div>
          <div class="love-box">
            <!-- v-if 假如item.topent为假就不执行执行v-else 反之真就执行 v-eles不执行 -->
            <img
              class="xing"
              v-if="item.topent"
              src="../assets/img/xing_on.png"
              @click="goto(index)"
            />
            <img
              class="xing"
              v-else
              src="../assets/img/xing_no.png"
              alt=""
              @click="goto(index)"
            />
            <span>{{ item.love }}</span>
          </div>
        </div>
      </div>
    </div>

    <div class="box" v-for="(i, index) in nelist" :key="index">
      <router-link :to="{ path: '/NoteDetail', query: { id: i.id } }">
        <div class="top-img">
          <img class="cai_img" :src="i.image" alt="" />
        </div>
      </router-link>
      <div class="but">
        <router-link :to="{ path: '/NoteDetail', query: { id: i.id } }">
          <span class="title"> {{ i.title }} </span>
        </router-link>
        <div class="but-user">
          <div class="but-user-name">
            <img class="ren_img" :src="i.userimg" alt="" />
            <span class="user">{{ i.username }}</span>
          </div>
          <div class="love-box">
            <img
              class="xing"
              v-if="i.inopent"
              src="../assets/img/xing_on.png"
              @click="foot(index)"
            />
            <img
              class="xing"
              v-else
              src="../assets/img/xing_no.png"
              alt=""
              @click="foot(index)"
            />
            <span>{{ i.love }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="kong"></div>
  </div>
</template>
<script>
import { getrecommend } from "../api/index";
export default {
  data() {
    return {
      list: [],
      nelist: [],
    };
  },
  methods: {
    goto(index) {
      // 将数据存储到本地
      let str = localStorage.getItem("list");
            let Arr = [];
            console.log(str);
            if (str) {
              Arr = JSON.parse(str);
            } else {
              Arr = [];
            }
            let arr = this.list[index];

      if (this.list[index].topent === false) {
        this.list[index].topent = true;
        this.list[index].love = this.list[index].love + 1;
        console.log(this.list[index]);
      
        let entry = {
          id:arr.id,
          arr
        };
      
        Arr.push(entry);
          console.log(Arr);
          
        localStorage.setItem("list", JSON.stringify(Arr));

      } else {
        this.list[index].topent = false;
        this.list[index].love = this.list[index].love - 1;
        let arrto = Arr.filter(item=>{
          return item.id!=arr.id
        })
        Arr=arrto
        localStorage.setItem("list",JSON.stringify(Arr))
      }
    },
    foot(index) {
      let str = localStorage.getItem("note");
            let Arr = [];
            if (str) {
              Arr = JSON.parse(str);
            } else {
              Arr = [];
            }
            let arr = this.nelist[index];
            console.log(arr);
      if (this.nelist[index].inopent === false) {
        this.nelist[index].inopent = true;
        this.nelist[index].love = this.nelist[index].love + 1;
        let entry = {
          id:arr.id,
          arr
        };
        Arr.push(entry);
          console.log(Arr);
          
        localStorage.setItem("note", JSON.stringify(Arr));
      } else {
        this.nelist[index].inopent = false;
        this.nelist[index].love = this.nelist[index].love - 1;
        let arrto = Arr.filter(item=>{
          return item.id!=arr.id
        })
        Arr=arrto
        localStorage.setItem("note",JSON.stringify(Arr))
      }
    },
  },
  async created() {
    await getrecommend().then((err) => {
      let {
        state,
        result: { list },
      } = err;
      console.log(err);
      if (state == "success") {
        // 过滤数据获得 菜谱内容
        let rlist = list.filter((i) => {
          return i.type == 1;
        });
        console.log(rlist);
        // 过滤获取 笔记内容
        let nlist = list.filter((i) => {
          return i.type == 3;
        });
        console.log(nlist);
        let arr = rlist.map((item) => {
          return {
            username: item?.r?.a?.n,
            userimg: item?.r?.a?.p,
            love: item?.r?.fc,
            title: item?.r?.n,
            image: item?.r?.img,
            id: item?.r?.id,
            topent: false,
          };
        });
        let Arr = nlist.map((item) => {
          return {
            username: item?.note?.author?.n,
            userimg: item?.note?.author?.p,
            love: item?.note?.like_count,
            title: item?.note?.title,
            image: item?.note?.image_u,
            id: item?.note?.id,
            inopent: false,
          };
        });
        this.list = arr;
        this.nelist = Arr;
      }
    });
  },
};
</script>

<style lang="less" scoped>
@import "../assets/gong.less";
.kong {
  width: 100%;
  height: 35px;
}
.recommend {
  background-color: rgb(241, 248, 248);
  display: flex;
  flex-wrap: wrap;
}

.box {
  width: 45%;
  height: 290px;
  margin: 10px 2%;
  border-radius: 10px;
  background-color: #fff;
  .top-img {
    .cai_img {
      width: 100%;
      height: 190px;
      border-top-right-radius: 10px;
      border-top-left-radius: 10px;
    }
  }
  .but {
    width: 100%;
    height: 30%;
    padding: 5px;
    .title {
      display: block;
      width: 100%;
      height: 60%;
      // background-color: rgb(211, 109, 40);
    }
    span {
      font-size: @zhufont;
    }
    .love-box {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 30%;
      span {
        margin-top: 5px;
      }
    }
    .but-user {
      .xing {
        width: 20px;
        height: 20px;
      }
      .user {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      span {
        font-size: 11px;
      }
      .ren_img {
        width: 20px;
        height: 20px;
        border-radius: 50%;
      }
      .but-user-name {
        width: 60%;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        span {
          margin-left: 5px;
        }
      }
      display: flex;
      line-height: 30px;
      align-items: center;
      justify-content: space-between;
    }
  }
}
</style>
